﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using TaoPortal.BusinessLogic.Repository;
using TaoPortal.Common;
using TaoPortal.Entity;


namespace TaoPortal.Areas.Admin.Models
{
    public class ArticleClassModel : BaseModel
    {
        public string ID { get; set; }
        public string ClassType { get; set; }
        [Required]
        [Display(Name = "分类名称")]
        public string ClassName { get; set; }
        public string ParentID { get; set; }
        public string ParentClassName { get; set; }
        [Required]
        [Display(Name = "排序")]
        public Nullable<int> Sort { get; set; }
        public string Status { get; set; }
        public string Remark { get; set; }

        public string ArticleClass { get; set; }

        public ArticleClassModel()
        {
            ArticleClass = "[]";
        }

        public void InitArticleClass()
        {
            var classList = GetClassByType(TaoPortal.Common.SysEnum.SysClassType.ArticleType);
            ArticleClass = TaoPortal.Common.Utility.JsonSerialization.SerializeObject(classList);
        }

        public void Add()
        {
            var rp = new SystemRepository();
            rp.AddAppClass(new TP_AppClass
            {
                ParentID = ParentID,
                ClassName = ClassName,
                ClassType = ((int)TaoPortal.Common.SysEnum.SysClassType.ArticleType).ToString(),
                Sort = Sort
            });
        }

        public void Edit()
        {
            var rp = new SystemRepository();
            rp.UpdateAppClass(new TP_AppClass
            {
                ID = this.ID,
                ClassName = this.ClassName,
                ParentID = this.ParentID,
                Sort = this.Sort
            });
        }

        public void Delete(string id)
        {
            var rp = new SystemRepository();
            rp.DeleteClass(id);
        }

        public void InitClass(string id)
        {
            var rp = new SystemRepository();
            var data = rp.GetClassByID(id);
            this.ID = data.ID;
            this.ClassName = data.ClassName;
            this.Sort = data.Sort;
            this.ParentID = data.ParentID;
        }

    }
}